<?php

require_once 'classes/LightPressPlugin.php';

class AuthorIndex extends LightPressPlugin {
    
    var $constructor_args = array(
        'posts_per_page'=>'number of items to display');
    var $default_context = LP_CONTEXT_AUTHOR;
    var $description = 'Modify the posts\' appearance in author\'s pages';
    

    var $active = true;
    var $hooks = array('get_posts', 'parse_post');
    var $posts_per_page = 30;

    function AuthorIndex(&$frontend, $args, $dummy_run=false) {
        
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        if (!$dummy_run) {
            $this->posts_per_page = (int)$this->posts_per_page;
            
            if ($frontend->context['type'] != 'author' || basename($_SERVER['PHP_SELF']) == 'feed.php') {
                $this->active = false;
                return;
            }
            $tpl =& $frontend->tpl;
            $db =& $frontend->db;
            
            $frontend->options['posts_per_page'] = $this->posts_per_page;
            
            $q = "select * from {$frontend->tables['users']} where id = {$frontend->context['filter']}";
            $db->query($q);
            
            $user = $db->next();
            
            foreach (array('user_pass', 'user_email') as $k) {
                if (isset($user[$k]))
                    unset($user[$k]);
            }
            
            $q = "select meta_key, meta_value
            from
                {$frontend->tables['usermeta']}
            where
                user_id = {$frontend->context['filter']}";

            if ($db->query($q) > 0) {
                foreach ($db->all() as $row) {
                    $k = "user_" . $row['meta_key'];
                    if (!isset($user[$k]))
                        $user[$k] = $row['meta_value'];
                }
            } else {
                return $this->hide();
            }
            
            $contacts = array();
            if (isset($user['user_url']) && !empty($user['user_url']) && $user['user_url'] != 'http://')
                $contacts[] = '<a href="' . $user['user_url'] . '">' . $user['user_url'] . '</a>';
            if (isset($user['user_email']) && !empty($user['user_email']))
                $contacts[] = '<a href="mailto:' . $user['user_email'] . '">' . $user['user_email'] . '</a>';
            if (isset($user['user_icq']) && !empty($user['user_icq']))
                $contacts[] = 'ICQ# ' . $user['user_icq'];
            if (isset($user['user_description']) && !empty($user['user_description']))
                $user['user_description'] = html_entity_decode($user['user_description']);
            else
                $user['user_description'] = '';
            $user['user_contacts'] = implode(' ~ ', $contacts);
            $user['display_name'] = $frontend->context['label'];
            
            $tpl->setVar($user);
            // build the feed URL
            $context_url = $frontend->context['url'];
            if ($frontend->raw_urls)
                $feed_url = str_replace('index.php', 'feed.php', $context_url);
            else
                $feed_url = $context_url . '/rss2.xml';
            $tpl->setVar('user_feed_url', $feed_url);
        }
        
    }

    function hide() {
        $tpl =& $this->_frontend->tpl;
        // play it on the safe side
        $tpl->setVar(array(                     
            'user_nickname'     => '',
            'user_num_posts'    => ''));
        if (isset($tpl->_files['index'])) {    // ugh
            $tpl->setBlock('index', 'user_details');
            $tpl->setVar('USER_DETAILS', '');
        }
    }
    
    function run($hook, &$post) {

        if ($hook == 'get_posts')
            return;
        $content = '';
        foreach (explode(' ', strip_tags($post['post_content'])) as $word) {
            if (strlen("$content $word") > 204)
                break;
            $content .= " $word";
        }
        $post['post_content'] = $content;
        $post['user_num_posts'] = $this->_frontend->num_posts;
        
    }

}

?>